# -*- coding: utf-8 -*-
'''
Created on Apr 4, 2013

@author: LONG HOANG GIANG
'''
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from urlparse import urljoin
import logging
import pyLib
import re
import json
import traceback
import workerpool


logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')

class Vficland():
    
    _config_stored_path = "/longhoanggiang/vficland/"
    
    def __init__(self):
        pyLib.createIfNotExistsPath(self._config_stored_path)
    
    def list(self, url):
        logging.debug("Call list method")
        tree = pyLib.loadWeb(url).build_tree()
        stories = []
        for liNode in tree.xpath("//div[@class='entry-content clearfix']/p/span/strong[contains(., 'Truyện dài kỳ')]/../../following-sibling::ul[1]/li/a".decode('utf-8')):
            storyName = pyLib.stringify(liNode)
            if storyName == '': continue
            storyLink = pyLib.normalizeStr(liNode.get('href', ''))
            if storyLink == '': continue
            storyLink = urljoin(url, storyLink)
            path = pyLib.extractText("/([^/]+)/$", storyLink, 1)
            print storyName, path, storyLink
            stories.append({'name': storyName, 'path': path, 'url': storyLink})
        return stories
        
    def getDetail(self, item, urlpath):
        logging.debug('> getDetail {0}'.format(item['url']))
        output = "{0}{1}/files/{2}".format(self._config_stored_path, urlpath, item['path'])
        if os.path.exists(output):
            return
        tree = pyLib.loadWeb(item['url']).build_tree()
        contentNode = tree.xpath("//div[@id='content']/div[contains(@id, 'post-')]/div[@class='entry-content clearfix']")[0]
        html = pyLib.cleanHTML(pyLib.Etree.tostring(contentNode), ['b', 'strong', 'hr'])
        print html
        html = re.sub(r"\n", "<br />", html)
        html = """<b>{0}</b><br /><hr /><br />{1}""".format(item['name'], html)
        
        pyLib.gzip(output, json.dumps(html))    
    
    def listChapters(self, sitem):
        logging.debug("Processing {0}".format(sitem['name']))
        tree = pyLib.loadWeb(sitem.get('url'), cached=True, referer='http://vficland.com/').build_tree()
        data = []
        nodes = tree.xpath("//ul[@class='lcp_catlist']/li/a")
        chapterIdx = 1
        for item in nodes:
            link = item.get('href', '')
            if link == '': continue
            chapterIdx += 1
        chapterIdx -= 1
        for item in nodes:
            try:
                title = Vficland.getTitle(pyLib.stringify(item))
                link = item.get('href', '')
                if link == '': continue
                link = urljoin(item.get('url'), link)
                print title, link
                fileName = pyLib.crc32unsigned(link)
                chapterItem = {'name': "CHƯƠNG {0}".format(chapterIdx), 'path': fileName, 'url': link}
                self.getDetail(chapterItem, sitem['path'])
                del chapterItem['url']
                data.append(chapterItem)
                chapterIdx -= 1
            except:
                traceback.print_exc()
        data.reverse()
        pyLib.gzip(self._config_stored_path + str(sitem['path']) + "/data", pyLib.encryptCipher(json.dumps(data)))
        
    @staticmethod
    def getTitle(strInput):
        return strInput
    
    def start(self, url):
        stories = self.list(url)
        pool = workerpool.WorkerPool(size=3)
        pool.map(self.listChapters, stories)
        pool.shutdown()
        pool.wait()
        
        for item in stories:
            del item['url']
        
        logging.info("OK! Done, ready save list of stories")
        pyLib.gzip(self._config_stored_path + "data", pyLib.encryptCipher(json.dumps(stories)))
        
    
    def getOne(self, url):
        if not url.endswith('/'): url += '/'
        path = pyLib.extractText(r"/([^/]+)/$", url, 1)
        self.listChapters({'name': '', 'path': path, 'url': url})
    
if __name__ == '__main__':
    
    vficland = Vficland()
    vficland.start('http://vficland.com/truyen-phuong-dong/')
#    vficland.getOne('http://vficland.com/truyen-phuong-dong/tay-om-con-tay-om-vo/')
#    vficland.getOne('http://vficland.com/truyen-phuong-dong/tay-om-con-tay-om-vo/')
    logging.info("Finished")
    os._exit(1)        